package com.zhp.service.impl;

import com.zhp.dao.UserDao;
import com.zhp.dao.UserFocueDao;
import com.zhp.dao.UserInfoDao;
import com.zhp.model.User;
import com.zhp.model.UserFocus;
import com.zhp.model.UserInfo;
import com.zhp.service.UserService;
import com.zhp.utils.MailUtil;
import com.zhp.utils.SrtUtil;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDao();
    private UserInfoDao userInfoDao = new UserInfoDao();
    private UserFocueDao userFocueDao = new UserFocueDao();

    @Override
    public List<User> findByT(Map<String, Object> map) {
        return userDao.findByT(map);

    }

    @Override
    public Boolean registerUser(String email, String username, String password) {
        User user = new User();
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setIsSecret("0");
        HashMap<String, Object> map = new HashMap<>();
        map.put("email", email);
        map.put("username", username);
        List<User> userList = userDao.findByT(map);
        if (userList == null || userList.size() == 0) {
            int row = userDao.save(user);
            if (row > 0) {
                UserInfo info = new UserInfo();
                List<User> userList2 = userDao.findByT(map);
                info.setUserId(userList2.get(0).getId());
                info.setRegisterTime(new Date());
                info.setRealName("IT大牛");
                if (userInfoDao.save(info) > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public String sendEmail(String email) {
        String code = SrtUtil.getRandomCode(4);
        //邮箱工具类
        String subject = "《小标交友》重置密码";
        String contrnt = "您的验证码是：" + code + "，有效期为10分钟。";
        try {
            MailUtil.send(email, subject, contrnt, MailUtil.UNICODE);
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return code;
    }

    @Override
    public boolean resetPassword(String email, String password) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("email", email);
        List<User> users = userDao.findByT(map);
        if (!users.isEmpty()) {
            User user = users.get(0);
            user.setPassword(password);
            return userDao.update(user) > 0;
        }
        return false;
    }

    @Override
    public Map<String, Object> findUserBaseInfo(String username) {
        Map<String, Object> baseInfoByName = userDao.findBaseInfoByName(username);
        return baseInfoByName;
    }

    @Override
    public Map<String, Object> getUserInfo(String id) {
        return userDao.getUserInfoId(id);
    }

    @Override
    public void updateLoginTime(Integer userId) {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("user_id", userId);
//        List<UserInfo> infos = userInfoDao.findByT(map);
        UserInfo info = userInfoDao.findById(userId);
        if (info != null) {
            info.setLoginTime(new Date());
            userInfoDao.update(info);
        }
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Map<String, Object>> getUserList(String userId, int currentPage, String name) {
        List<Map<String, Object>> users = userDao.findUsers(userId, currentPage, name);
        return users;
    }

    @Override
    public long getUserCount(String name) {
        return userDao.getUserTotal(name);
    }

    @Override
    public boolean followUser(String userId, String ufid) {
        UserFocus userFocus = new UserFocus();
        userFocus.setUserId(Integer.valueOf(userId));
        userFocus.setUserFocusId(Integer.valueOf(ufid));
        return userFocueDao.save(userFocus) > 0;
    }

    @Override
    public UserFocus getFollowUser(String userId, String ufid) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("user_focus_id", ufid);
        List<UserFocus> focusList = userFocueDao.findByTAnd(map);
        if (!focusList.isEmpty()) {
            return focusList.get(0);
        }
        return null;
    }

    @Override
    public boolean unFollowUser(Integer id) {
        return userFocueDao.deleteById(id) > 0;
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public void uploadImg(String userId,String path) {
        UserInfo userInfo =  userInfoDao.findById(Integer.parseInt(userId));
        userInfo.setPic(path);
        userInfoDao.update(userInfo);
    }

    @Override
    public List<User> getFocus(String id,int page) {
        return  userDao.findMyFocus(id,page);
    }

    @Override
    public Number getFocusAll(String id) {
        return userDao.findMyFocusAll(id);
    }
}
